package org.pzy.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @Author: DRevelation
 * @Date: 2022/5/25 10:07
 */
public class JobDemo {

    public static void main(String[] args) throws SchedulerException, InterruptedException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();

        JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
                .withIdentity("my-job")
                .usingJobData("job-params", "job参数")
                .build();

        // 构建trigger，每30秒执行一次
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(30)
                        .repeatForever())
                .usingJobData("trigger-params", "trigger参数")
                .build();

        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();

        TimeUnit.MINUTES.sleep(1);
        scheduler.shutdown();
    }
}
